<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.2.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"llmysnow.top","root":"/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":true,"show_result":true,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":true},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":true,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="自动化构建源代码 -&gt; 生成代码">
<meta property="og:type" content="article">
<meta property="og:title" content="自动化构建（Grunt、Gulp）">
<meta property="og:url" content="http://llmysnow.top/posts/95392a2e/index.html">
<meta property="og:site_name" content="奇幻的梦之旅">
<meta property="og:description" content="自动化构建源代码 -&gt; 生成代码">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://gitee.com/lilyn/pic/raw/master/lagoulearn-img/%E5%89%8D%E7%AB%AF%E6%A0%B7%E5%BC%8F%E8%87%AA%E5%8A%A8%E5%8C%96%E6%9E%84%E5%BB%BA.png">
<meta property="og:image" content="https://gitee.com/lilyn/pic/raw/master/lagoulearn-img/%E5%B8%B8%E7%94%A8%E8%87%AA%E5%8A%A8%E5%8C%96%E6%9E%84%E5%BB%BA%E5%B7%A5%E5%85%B7.png">
<meta property="og:image" content="https://gitee.com/lilyn/pic/raw/master/lagoulearn-img/Gulp%E6%A0%B8%E5%BF%83%E5%B7%A5%E4%BD%9C%E5%8E%9F%E7%90%86.png">
<meta property="og:image" content="https://gitee.com/lilyn/pic/raw/master/lagoulearn-img/gulp%E8%87%AA%E5%8A%A8%E5%8C%96%E5%B7%A5%E4%BD%9C%E6%B5%81%E6%9E%84%E5%BB%BA.png">
<meta property="article:published_time" content="2022-04-23T11:27:04.415Z">
<meta property="article:modified_time" content="2022-04-23T11:27:47.143Z">
<meta property="article:author" content="LiLyn">
<meta property="article:tag" content="Grunt">
<meta property="article:tag" content="Gulp">
<meta property="article:tag" content="封装工作流">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gitee.com/lilyn/pic/raw/master/lagoulearn-img/%E5%89%8D%E7%AB%AF%E6%A0%B7%E5%BC%8F%E8%87%AA%E5%8A%A8%E5%8C%96%E6%9E%84%E5%BB%BA.png">

<link rel="canonical" href="http://llmysnow.top/posts/95392a2e/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>自动化构建（Grunt、Gulp） | 奇幻的梦之旅</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

<link rel="alternate" href="/atom.xml" title="奇幻的梦之旅" type="application/atom+xml">
</head>
<!--即时预加载技术，在用户点击之前预先加载页面-->
<script src="https://cdn.jsdelivr.net/gh/TRHX/CDN-for-itrhx.com@2.0.2/js/instantclick-1.2.2.js" type="module"></script>
<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>
	  <!--<a target="_blank" rel="noopener" href="https://github.com/llwodexue" class="github-corner" aria-label="View source on GitHub"><svg width="80" height="80" viewBox="0 0 250 250" style="fill:#151513; color:#fff; position: absolute; top: 0; border: 0; right: 0;" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a><style>.github-corner:hover .octo-arm{animation:octocat-wave 560ms ease-in-out}@keyframes octocat-wave{0%,100%{transform:rotate(0)}20%,60%{transform:rotate(-25deg)}40%,80%{transform:rotate(10deg)}}@media (max-width:500px){.github-corner:hover .octo-arm{animation:none}.github-corner .octo-arm{animation:octocat-wave 560ms ease-in-out}}</style>-->
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">奇幻的梦之旅</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">http://llmysnow.top/</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://llmysnow.top/posts/95392a2e/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/logo.jpg">
      <meta itemprop="name" content="LiLyn">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="奇幻的梦之旅">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          自动化构建（Grunt、Gulp）
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2022-04-23 19:27:04" itemprop="dateCreated datePublished" datetime="2022-04-23T19:27:04+08:00">2022-04-23</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E5%B7%A5%E7%A8%8B%E5%8C%96/" itemprop="url" rel="index"><span itemprop="name">工程化</span></a>
                </span>
            </span>

          <br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="far fa-file-word"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>23k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="far fa-clock"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>21 分钟</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h2 id="自动化构建"><a href="#自动化构建" class="headerlink" title="自动化构建"></a>自动化构建</h2><p><strong>源代码 -&gt; 生成代码</strong></p>
<p><img data-src="https://gitee.com/lilyn/pic/raw/master/lagoulearn-img/%E5%89%8D%E7%AB%AF%E6%A0%B7%E5%BC%8F%E8%87%AA%E5%8A%A8%E5%8C%96%E6%9E%84%E5%BB%BA.png" alt="前端样式自动化构建"></p>
<a id="more"></a>

<p>输入 <code>scss scss/main.scss css/style.css</code> 即可构建出 css</p>
<ul>
<li><p>缺点：每次都需要重复输入这个复杂的命令</p>
<p>npm script 就是用来解决这个问题</p>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  <span class="attr">&quot;scripts&quot;</span>: &#123;</span><br><span class="line">    <span class="attr">&quot;build&quot;</span>: <span class="string">&quot;sass scss/main.scss css/style.css --watch&quot;</span>,</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

</li>
</ul>
<p>接下来安装 <code>browser-sync</code> 用于启动测试服务器</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yarn add browser-sync --dev</span><br></pre></td></tr></table></figure>

<p>运行测试服务器 <code>npm run serve</code></p>
<ul>
<li><code>--files</code> 监听文件变化</li>
</ul>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  <span class="attr">&quot;scripts&quot;</span>: &#123;</span><br><span class="line">    <span class="attr">&quot;serve&quot;</span>: <span class="string">&quot;browser-sync . --files \&quot;css/*.css\&quot;</span></span><br><span class="line"><span class="string">  &#125;,</span></span><br><span class="line"><span class="string">&#125;</span></span><br></pre></td></tr></table></figure>

<p>不过这里可能有个问题，如果在运行测试服务器之前，没有生成 css，浏览器就没办法展示样式</p>
<ul>
<li>这里就可以用到 npm script 的钩子机制 <code>preserve</code> 在启动任务之前，让 build 先去工作</li>
</ul>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  <span class="attr">&quot;scripts&quot;</span>: &#123;</span><br><span class="line">    <span class="attr">&quot;preserve&quot;</span>: <span class="string">&quot;yarn build&quot;</span></span><br><span class="line">  &#125;,</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>如果给 build 增加 <code>--watch</code> 参数，scss 会阻塞命令行等待文件变化，就会导致 <code>browser-sync</code> 没办法直接去工作</p>
<ul>
<li><p>这里就需要用 <code>npm-run-all</code> 同时执行多个命令</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yarn add npm-run-all --dev</span><br></pre></td></tr></table></figure>

</li>
</ul>
<figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">  <span class="attr">&quot;scripts&quot;</span>: &#123;</span><br><span class="line">    <span class="attr">&quot;start&quot;</span>: <span class="string">&quot;run-p build serve&quot;</span></span><br><span class="line">  &#125;,</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>常用工具</strong></p>
<ul>
<li>严格来说 Webpack 是模块打包工具</li>
<li>Grunt、Gulp、FIS 是自动化构建工具</li>
</ul>
<p><img data-src="https://gitee.com/lilyn/pic/raw/master/lagoulearn-img/%E5%B8%B8%E7%94%A8%E8%87%AA%E5%8A%A8%E5%8C%96%E6%9E%84%E5%BB%BA%E5%B7%A5%E5%85%B7.png" alt="常用自动化构建工具"></p>
<ul>
<li>Grunt 老牌构建工具，插件生态完善，但是它的工作过程是基于临时文件实现的，所以构建速度较慢(Scss 编译后就会将结果写入到临时文件，下一个插件读取这个临时文件进行操作，环节越多读取文件次数越多)</li>
<li>Gulp 基于内存实现的，对文件的处理都是在内存中实现，速度快一些，默认同时支持多个任务，目前比较流行</li>
<li>FIS 百度团队研发，把项目中最典型需求都集成到内部，大而全</li>
</ul>
<h2 id="Grunt"><a href="#Grunt" class="headerlink" title="Grunt"></a>Grunt</h2><h3 id="基本使用"><a href="#基本使用" class="headerlink" title="基本使用"></a>基本使用</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">yarn init –yes</span><br><span class="line">yarn add grunt</span><br></pre></td></tr></table></figure>

<p>新建 <code>gruntfile.js</code></p>
<ul>
<li>Grunt 的入口文件，用于定义一些需要 Grunt 自动执行的任务</li>
<li>需要导出一个函数，该函数接收一个 grunt 对象，内部提供一些创建任务可以用到的 API</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">module</span>.exports = <span class="function"><span class="params">grunt</span> =&gt;</span> &#123;</span><br><span class="line">  grunt.registerTask(<span class="string">&#x27;foo&#x27;</span>, <span class="string">&#x27;任务描述&#x27;</span>, <span class="function">() =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;foo&#x27;</span>)</span><br><span class="line">  &#125;)</span><br><span class="line">  grunt.registerTask(<span class="string">&#x27;bar&#x27;</span>, <span class="function">() =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;bar&#x27;</span>)</span><br><span class="line">  &#125;)</span><br><span class="line">  <span class="comment">// 第二个参数可以指定此任务的映射任务</span></span><br><span class="line">  <span class="comment">// 这样执行 default 就相当于执行对应的任务</span></span><br><span class="line">  <span class="comment">// 这里映射的任务会按顺序依次执行，不会同步执行</span></span><br><span class="line">  grunt.registerTask(<span class="string">&#x27;default&#x27;</span>, [<span class="string">&#x27;foo&#x27;</span>, <span class="string">&#x27;bar&#x27;</span>])</span><br><span class="line">  <span class="comment">// 由于函数体中需要使用 this，所以这里不能使用箭头函数</span></span><br><span class="line">  grunt.registerTask(<span class="string">&#x27;async-task&#x27;</span>, <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">const</span> done = <span class="built_in">this</span>.async()</span><br><span class="line">    <span class="built_in">setTimeout</span>(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">      <span class="built_in">console</span>.log(<span class="string">&#x27;async task working~&#x27;</span>)</span><br><span class="line">      done()</span><br><span class="line">    &#125;, <span class="number">1000</span>)</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="标记任务失败"><a href="#标记任务失败" class="headerlink" title="标记任务失败"></a>标记任务失败</h3><ul>
<li><p>任务函数执行过程中如果返回 false，则意味着此任务执行失败</p>
</li>
<li><p>如果一个任务列表中的某个任务执行失败，则后续任务默认不会运行</p>
<p>除非 grunt 运行时指定 <code>--force</code> 参数强制执行</p>
</li>
<li><p>异步函数中标记当前任务执行失败的方式是为回调函数指定一个 false 的实参</p>
</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">module</span>.exports = <span class="function"><span class="params">grunt</span> =&gt;</span> &#123;</span><br><span class="line">  grunt.registerTask(<span class="string">&#x27;bad&#x27;</span>, <span class="function">() =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;bad working~&#x27;</span>)</span><br><span class="line">    <span class="keyword">return</span> <span class="literal">false</span></span><br><span class="line">  &#125;)</span><br><span class="line">  grunt.registerTask(<span class="string">&#x27;foo&#x27;</span>, <span class="function">() =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;foo working~&#x27;</span>)</span><br><span class="line">  &#125;)</span><br><span class="line">  grunt.registerTask(<span class="string">&#x27;bar&#x27;</span>, <span class="function">() =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;bar working~&#x27;</span>)</span><br><span class="line">  &#125;)</span><br><span class="line">  grunt.registerTask(<span class="string">&#x27;default&#x27;</span>, [<span class="string">&#x27;foo&#x27;</span>, <span class="string">&#x27;bad&#x27;</span>, <span class="string">&#x27;bar&#x27;</span>])</span><br><span class="line">  grunt.registerTask(<span class="string">&#x27;bad-async&#x27;</span>, <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">const</span> done = <span class="built_in">this</span>.async()</span><br><span class="line">    <span class="built_in">setTimeout</span>(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">      <span class="built_in">console</span>.log(<span class="string">&#x27;async task working~&#x27;</span>)</span><br><span class="line">      done(<span class="literal">false</span>)</span><br><span class="line">    &#125;, <span class="number">1000</span>)</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="配置选项方法"><a href="#配置选项方法" class="headerlink" title="配置选项方法"></a>配置选项方法</h3><p>用 <code>init</code> 配置，用 <code>grunt.config</code> 获取属性值</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">module</span>.exports = <span class="function"><span class="params">grunt</span> =&gt;</span> &#123;</span><br><span class="line">  grunt.initConfig(&#123;</span><br><span class="line">    foo: &#123;</span><br><span class="line">      bar: <span class="number">123</span>,</span><br><span class="line">    &#125;,</span><br><span class="line">  &#125;)</span><br><span class="line">  grunt.registerTask(<span class="string">&#x27;foo&#x27;</span>, <span class="function">() =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(grunt.config(<span class="string">&#x27;foo.bar&#x27;</span>))</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="多目标任务"><a href="#多目标任务" class="headerlink" title="多目标任务"></a>多目标任务</h3><p>用 <code>init</code> 配置，通过 <code>this</code> 拿到执行的名称和数据</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">module</span>.exports = <span class="function"><span class="params">grunt</span> =&gt;</span> &#123;</span><br><span class="line">  <span class="comment">// 多目标模式，可以让任务根据配置形成多个子任务</span></span><br><span class="line">  grunt.initConfig(&#123;</span><br><span class="line">    build: &#123;</span><br><span class="line">      options: &#123;</span><br><span class="line">        msg: <span class="string">&#x27;task options&#x27;</span>,</span><br><span class="line">      &#125;,</span><br><span class="line">      foo: &#123;</span><br><span class="line">        options: &#123;</span><br><span class="line">          msg: <span class="string">&#x27;foo target options&#x27;</span>,</span><br><span class="line">        &#125;,</span><br><span class="line">      &#125;,</span><br><span class="line">      bar: <span class="string">&#x27;456&#x27;</span>,</span><br><span class="line">    &#125;,</span><br><span class="line">  &#125;)</span><br><span class="line">  grunt.registerMultiTask(<span class="string">&#x27;build&#x27;</span>, <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="built_in">this</span>.options())</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">`target:<span class="subst">$&#123;<span class="built_in">this</span>.target&#125;</span>,data:<span class="subst">$&#123;<span class="built_in">this</span>.data&#125;</span>`</span>)</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="Grunt-插件"><a href="#Grunt-插件" class="headerlink" title="Grunt 插件"></a>Grunt 插件</h3><p>安装 <code>grunt-contrib-clean</code></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yarn add grunt-contrib-clean --dev</span><br></pre></td></tr></table></figure>

<p>将 temp 目录下所有文件都清除</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">module.exports = grunt =&gt; &#123;</span><br><span class="line">  grunt.initConfig(&#123;</span><br><span class="line">    clean: &#123;</span><br><span class="line">      temp: <span class="string">&#x27;temp/**&#x27;</span>,</span><br><span class="line">    &#125;,</span><br><span class="line">  &#125;)</span><br><span class="line"></span><br><span class="line">  grunt.loadNpmTasks(<span class="string">&#x27;grunt-contrib-clean&#x27;</span>)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>安装 <code>grunt-sass</code>、<code>sass</code></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yarn add grunt-sass sass --dev</span><br></pre></td></tr></table></figure>

<p>安装 <code>grunt-babal</code>、<code>babel</code></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yarn add grunt-babel @babel/core @babel/preset-env --dev</span><br></pre></td></tr></table></figure>

<p>随着 <code>gruntfile.js</code> 越来越复杂，<code>loadNpmTasks</code> 操作也会越来越多，可以使用 <code>load-grunt-tasks</code></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yarn add load-grunt-tasks --dev</span><br></pre></td></tr></table></figure>

<p>当文件修改完，需要自动去编译，这时需要另外一个插件 <code>grunt-contrib-watch</code></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yarn add grunt-contrib-watch --dev</span><br></pre></td></tr></table></figure>

<p>Grunt 使用插件</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> sass = <span class="built_in">require</span>(<span class="string">&#x27;sass&#x27;</span>)</span><br><span class="line"><span class="keyword">const</span> loadGruntTasks = <span class="built_in">require</span>(<span class="string">&#x27;load-grunt-tasks&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="built_in">module</span>.exports = <span class="function"><span class="params">grunt</span> =&gt;</span> &#123;</span><br><span class="line">  grunt.initConfig(&#123;</span><br><span class="line">    sass: &#123;</span><br><span class="line">      options: &#123;</span><br><span class="line">        sourceMap: <span class="literal">true</span>,</span><br><span class="line">        implementation: sass,</span><br><span class="line">      &#125;,</span><br><span class="line">      main: &#123;</span><br><span class="line">        files: &#123;</span><br><span class="line">          <span class="string">&#x27;dist/css/main.css&#x27;</span>: <span class="string">&#x27;src/scss/main.scss&#x27;</span>,</span><br><span class="line">        &#125;,</span><br><span class="line">      &#125;,</span><br><span class="line">    &#125;,</span><br><span class="line">    babel: &#123;</span><br><span class="line">      options: &#123;</span><br><span class="line">        sourceMap: <span class="literal">true</span>,</span><br><span class="line">        presets: [<span class="string">&#x27;@babel/preset-env&#x27;</span>],</span><br><span class="line">      &#125;,</span><br><span class="line">      main: &#123;</span><br><span class="line">        files: &#123;</span><br><span class="line">          <span class="string">&#x27;dist/js/app.js&#x27;</span>: <span class="string">&#x27;src/js/app.js&#x27;</span>,</span><br><span class="line">        &#125;,</span><br><span class="line">      &#125;,</span><br><span class="line">    &#125;,</span><br><span class="line">    watch: &#123;</span><br><span class="line">      js: &#123;</span><br><span class="line">        files: [<span class="string">&#x27;src/js/*.js&#x27;</span>],</span><br><span class="line">        tasks: [<span class="string">&#x27;babel&#x27;</span>],</span><br><span class="line">      &#125;,</span><br><span class="line">      css: &#123;</span><br><span class="line">        files: [<span class="string">&#x27;src/scss/*.scss&#x27;</span>],</span><br><span class="line">        tasks: [<span class="string">&#x27;sass&#x27;</span>],</span><br><span class="line">      &#125;,</span><br><span class="line">    &#125;,</span><br><span class="line">  &#125;)</span><br><span class="line"></span><br><span class="line">  loadGruntTasks(grunt) <span class="comment">// 自动加载所有 grunt 插件中的任务</span></span><br><span class="line">  grunt.registerTask(<span class="string">&#x27;default&#x27;</span>, [<span class="string">&#x27;sass&#x27;</span>, <span class="string">&#x27;babel&#x27;</span>, <span class="string">&#x27;watch&#x27;</span>])</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="Gulp"><a href="#Gulp" class="headerlink" title="Gulp"></a>Gulp</h2><h3 id="基本使用-1"><a href="#基本使用-1" class="headerlink" title="基本使用"></a>基本使用</h3><p>Gulp 核心特点：高效易用</p>
<ul>
<li>安装 Gulp 同时会自动安装一个叫 gulpcli 模块</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">yarn init --yes</span><br><span class="line">yarn add gulp --dev</span><br></pre></td></tr></table></figure>

<p>创建 <code>gulpfile.js</code> 入口，导出一个 foo 函数</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">exports</span>.foo = <span class="function">() =&gt;</span> &#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">&#x27;foo task working~&#x27;</span>)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>执行 <code>yarn gulp foo</code>,发现有报错</p>
<ul>
<li>gulp 取消了同步代码模式，约定每一个任务都是异步的，当任务执行完需要通过回调函数标记任务执行完毕</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">[11:04:54] The following tasks did not complete: foo</span><br><span class="line">[11:04:54] Did you forget to signal async completion?</span><br><span class="line">error Command failed with <span class="built_in">exit</span> code 1.</span><br></pre></td></tr></table></figure>

<p>gulp 4.0 以后的保留了 <code>gulp.task</code> API</p>
<ul>
<li>gulp 4.0 以后推荐使用导出函数成员方式</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">exports</span>.foo = <span class="function"><span class="params">done</span> =&gt;</span> &#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">&#x27;foo task working~&#x27;</span>)</span><br><span class="line">  done() <span class="comment">// 标识任务执行完成</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// default 是默认任务</span></span><br><span class="line"><span class="built_in">exports</span>.default = <span class="function"><span class="params">done</span> =&gt;</span> &#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">&#x27;default task working~&#x27;</span>)</span><br><span class="line">  done()</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// v4.0 之前需要通过 gulp.task() 方法注册任务</span></span><br><span class="line"><span class="keyword">const</span> gulp = <span class="built_in">require</span>(<span class="string">&#x27;gulp&#x27;</span>)</span><br><span class="line">gulp.task(<span class="string">&#x27;bar&#x27;</span>, <span class="function"><span class="params">done</span> =&gt;</span> &#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">&#x27;bar task working~&#x27;</span>)</span><br><span class="line">  done()</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<h3 id="组合任务"><a href="#组合任务" class="headerlink" title="组合任务"></a>组合任务</h3><p>通过 <code>series</code>、<code>parallel</code> 组合多个任务</p>
<ul>
<li>编译 CSS、JS 任务互不干扰可以并行处理</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> &#123; series, parallel &#125; = <span class="built_in">require</span>(<span class="string">&#x27;gulp&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> task1 = <span class="function"><span class="params">done</span> =&gt;</span> &#123;</span><br><span class="line">  <span class="built_in">setTimeout</span>(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;task1 working~&#x27;</span>)</span><br><span class="line">    done()</span><br><span class="line">  &#125;, <span class="number">1000</span>)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> task2 = <span class="function"><span class="params">done</span> =&gt;</span> &#123;</span><br><span class="line">  <span class="built_in">setTimeout</span>(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;task2 working~&#x27;</span>)</span><br><span class="line">    done()</span><br><span class="line">  &#125;, <span class="number">1000</span>)  </span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> task3 = <span class="function"><span class="params">done</span> =&gt;</span> &#123;</span><br><span class="line">  <span class="built_in">setTimeout</span>(<span class="function">() =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">&#x27;task3 working~&#x27;</span>)</span><br><span class="line">    done()</span><br><span class="line">  &#125;, <span class="number">1000</span>)  </span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 让多个任务按照顺序依次执行</span></span><br><span class="line"><span class="built_in">exports</span>.foo = series(task1, task2, task3)</span><br><span class="line"><span class="comment">// 让多个任务同时执行</span></span><br><span class="line"><span class="built_in">exports</span>.bar = parallel(task1, task2, task3)</span><br></pre></td></tr></table></figure>

<h3 id="异步任务"><a href="#异步任务" class="headerlink" title="异步任务"></a>异步任务</h3><ol>
<li>回调函数</li>
<li>Promsie、async 和 await（Node 8 以上的版本）</li>
<li>Stram 方式</li>
</ol>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> fs = <span class="built_in">require</span>(<span class="string">&#x27;fs&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="built_in">exports</span>.callback = <span class="function"><span class="params">done</span> =&gt;</span> &#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">&#x27;callback task&#x27;</span>)</span><br><span class="line">  done()</span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">exports</span>.callback_error = <span class="function"><span class="params">done</span> =&gt;</span> &#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">&#x27;callback task&#x27;</span>)</span><br><span class="line">  done(<span class="keyword">new</span> <span class="built_in">Error</span>(<span class="string">&#x27;task failed&#x27;</span>))</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="built_in">exports</span>.promise = <span class="function">() =&gt;</span> &#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">&#x27;promise task&#x27;</span>)</span><br><span class="line">  <span class="keyword">return</span> <span class="built_in">Promise</span>.resolve()</span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">exports</span>.promise_error = <span class="function">() =&gt;</span> &#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">&#x27;promise task&#x27;</span>)</span><br><span class="line">  <span class="keyword">return</span> <span class="built_in">Promise</span>.reject(<span class="keyword">new</span> <span class="built_in">Error</span>(<span class="string">&#x27;task failed&#x27;</span>))</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> timeout = <span class="function"><span class="params">time</span> =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="keyword">new</span> <span class="built_in">Promise</span>(<span class="function"><span class="params">resolve</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">setTimeout</span>(resolve, time)</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">exports</span>.async = <span class="keyword">async</span> () =&gt; &#123;</span><br><span class="line">  <span class="keyword">await</span> timeout(<span class="number">1000</span>)</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">&#x27;async task&#x27;</span>)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="built_in">exports</span>.stream = <span class="function">() =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">const</span> readStream = fs.createReadStream(<span class="string">&#x27;package.json&#x27;</span>)</span><br><span class="line">  <span class="keyword">const</span> writeStream = fs.createWriteStream(<span class="string">&#x27;temp.txt&#x27;</span>)</span><br><span class="line">  readStream.pipe(writeStream) <span class="comment">// pipe导入写入流中</span></span><br><span class="line">  <span class="comment">// gulp 只是注册了 end 事件去结束</span></span><br><span class="line">  <span class="comment">/* readStream.on(&#x27;end&#x27;, () =&gt; &#123;</span></span><br><span class="line"><span class="comment">    done()</span></span><br><span class="line"><span class="comment">  &#125;) */</span></span><br><span class="line">  <span class="keyword">return</span> readStream</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="核心工作原理"><a href="#核心工作原理" class="headerlink" title="核心工作原理"></a>核心工作原理</h3><ul>
<li>The streaming build system</li>
</ul>
<p>stream 的 Transform 类型可以创建文件转换流对象，里面需要指定 transform 属性（转化流核心转换过程）</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> fs = <span class="built_in">require</span>(<span class="string">&#x27;fs&#x27;</span>)</span><br><span class="line"><span class="keyword">const</span> &#123; Transform &#125; = <span class="built_in">require</span>(<span class="string">&#x27;stream&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="built_in">exports</span>.default = <span class="function">() =&gt;</span> &#123;</span><br><span class="line">  <span class="comment">// 文件读取流</span></span><br><span class="line">  <span class="keyword">const</span> readStream = fs.createReadStream(<span class="string">&#x27;normalize.css&#x27;</span>)</span><br><span class="line">  <span class="comment">// 文件写入流</span></span><br><span class="line">  <span class="keyword">const</span> writeStream = fs.createWriteStream(<span class="string">&#x27;normalize.min.css&#x27;</span>)</span><br><span class="line">  <span class="comment">// 文件转换流</span></span><br><span class="line">  <span class="keyword">const</span> transformStream = <span class="keyword">new</span> Transform(&#123;</span><br><span class="line">    <span class="comment">// 核心转换过程</span></span><br><span class="line">    transform: <span class="function">(<span class="params">chunk, encoding, callback</span>) =&gt;</span> &#123;</span><br><span class="line">      <span class="keyword">const</span> input = chunk.toString()</span><br><span class="line">      <span class="keyword">const</span> output = input.replace(<span class="regexp">/\s+/g</span>, <span class="string">&#x27;&#x27;</span>).replace(<span class="regexp">/\/\*.+?\*\//g</span>, <span class="string">&#x27;&#x27;</span>)</span><br><span class="line">      callback(<span class="literal">null</span>, output)</span><br><span class="line">    &#125;,</span><br><span class="line">  &#125;)</span><br><span class="line"></span><br><span class="line">  <span class="keyword">return</span> readStream</span><br><span class="line">    .pipe(transformStream) <span class="comment">// 转换</span></span><br><span class="line">    .pipe(writeStream) <span class="comment">// 写入</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><img data-src="https://gitee.com/lilyn/pic/raw/master/lagoulearn-img/Gulp%E6%A0%B8%E5%BF%83%E5%B7%A5%E4%BD%9C%E5%8E%9F%E7%90%86.png" alt="Gulp核心工作原理"></p>
<h3 id="文件操作-API"><a href="#文件操作-API" class="headerlink" title="文件操作 API"></a>文件操作 API</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">yarn add gulp-clean-css --dev</span><br><span class="line">yarn add gulp-rename --dev</span><br></pre></td></tr></table></figure>

<p>通过 src 去 pipe 到插件转换流，再去 pipe 到写入流的过程就是使用 gulp 常规过程</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> &#123; src, dest &#125; = <span class="built_in">require</span>(<span class="string">&#x27;gulp&#x27;</span>)</span><br><span class="line"><span class="keyword">const</span> cleanCSS = <span class="built_in">require</span>(<span class="string">&#x27;gulp-clean-css&#x27;</span>)</span><br><span class="line"><span class="keyword">const</span> rename = <span class="built_in">require</span>(<span class="string">&#x27;gulp-rename&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="built_in">exports</span>.default = <span class="function">() =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">return</span> src(<span class="string">&#x27;src/*.css&#x27;</span>)</span><br><span class="line">    .pipe(cleanCSS())</span><br><span class="line">    .pipe(rename(&#123; <span class="attr">extname</span>: <span class="string">&#x27;.min.css&#x27;</span> &#125;))</span><br><span class="line">    .pipe(dest(<span class="string">&#x27;dist&#x27;</span>))</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="Gulp-案例"><a href="#Gulp-案例" class="headerlink" title="Gulp 案例"></a>Gulp 案例</h2><h3 id="样式编译"><a href="#样式编译" class="headerlink" title="样式编译"></a>样式编译</h3><p>下载代码</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">git <span class="built_in">clone</span> https://github.com/zec/zec-gulp-demo.git</span><br></pre></td></tr></table></figure>

<p>安装 <code>gulp</code>、<code>gulp-sass</code></p>
<ul>
<li>安装 <code>gulp-sass</code> 会安装 node-sass（是 C++ 模块），内部会有对 C++ 程序集的依赖，二进制包需要国外站点去下载，有时会下载不下来，可以配置淘宝镜像源下载</li>
</ul>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">yarn add gulp --dev</span><br><span class="line">yarn add gulp-sass --dev</span><br></pre></td></tr></table></figure>

<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> &#123; src, dest &#125; = <span class="built_in">require</span>(<span class="string">&#x27;gulp&#x27;</span>)</span><br><span class="line"><span class="keyword">const</span> sass = <span class="built_in">require</span>(<span class="string">&#x27;gulp-sass&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> style = <span class="function">() =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">return</span> src(<span class="string">`src/assets/styles/*.scss`</span>, &#123; <span class="attr">base</span>: <span class="string">&#x27;src&#x27;</span> &#125;)</span><br><span class="line">    .pipe(sass(&#123; <span class="attr">outputStyle</span>: <span class="string">&#x27;expanded&#x27;</span> &#125;))</span><br><span class="line">    .pipe(dest(<span class="string">&#x27;dist&#x27;</span>))</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="built_in">module</span>.exports = &#123;</span><br><span class="line">  style,</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="脚本编译"><a href="#脚本编译" class="headerlink" title="脚本编译"></a>脚本编译</h3><p>安装 <code>gulp-babel</code>、<code>@babel/core</code>、<code>@babel/preset-env</code></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">yarn add gulp-babel --dev</span><br><span class="line">yarn add @babel/core @babel/preset-env --dev</span><br></pre></td></tr></table></figure>

<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> &#123; src, dest &#125; = <span class="built_in">require</span>(<span class="string">&#x27;gulp&#x27;</span>)</span><br><span class="line"><span class="keyword">const</span> babel = <span class="built_in">require</span>(<span class="string">&#x27;gulp-babel&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> script = <span class="function">() =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">return</span> src(<span class="string">`src/assets/scripts/*.js`</span>, &#123; <span class="attr">base</span>: <span class="string">&#x27;src&#x27;</span> &#125;)</span><br><span class="line">    .pipe(babel(&#123; <span class="attr">presets</span>: [<span class="string">&#x27;@babel/preset-env&#x27;</span>] &#125;))</span><br><span class="line">    .pipe(dest(<span class="string">&#x27;dist&#x27;</span>))</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="built_in">module</span>.exports = &#123;</span><br><span class="line">  script,</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="页面模板编译"><a href="#页面模板编译" class="headerlink" title="页面模板编译"></a>页面模板编译</h3><p>安装 <code>gulp-swig</code></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yarn add gulp-swig --dev</span><br></pre></td></tr></table></figure>

<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> &#123; src, dest, parallel &#125; = <span class="built_in">require</span>(<span class="string">&#x27;gulp&#x27;</span>)</span><br><span class="line"><span class="keyword">const</span> swig = <span class="built_in">require</span>(<span class="string">&#x27;gulp-swig&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> data = &#123;</span><br><span class="line">  menus: [</span><br><span class="line">    &#123;</span><br><span class="line">      name: <span class="string">&#x27;Home&#x27;</span>,</span><br><span class="line">      icon: <span class="string">&#x27;aperture&#x27;</span>,</span><br><span class="line">      link: <span class="string">&#x27;index.html&#x27;</span>,</span><br><span class="line">    &#125;,</span><br><span class="line">    &#123;</span><br><span class="line">      name: <span class="string">&#x27;Features&#x27;</span>,</span><br><span class="line">      link: <span class="string">&#x27;features.html&#x27;</span>,</span><br><span class="line">    &#125;,</span><br><span class="line">    &#123;</span><br><span class="line">      name: <span class="string">&#x27;About&#x27;</span>,</span><br><span class="line">      link: <span class="string">&#x27;about.html&#x27;</span>,</span><br><span class="line">    &#125;,</span><br><span class="line">    &#123;</span><br><span class="line">      name: <span class="string">&#x27;Contact&#x27;</span>,</span><br><span class="line">      link: <span class="string">&#x27;#&#x27;</span>,</span><br><span class="line">      children: [</span><br><span class="line">        &#123;</span><br><span class="line">          name: <span class="string">&#x27;Twitter&#x27;</span>,</span><br><span class="line">          link: <span class="string">&#x27;https://twitter.com/w_zce&#x27;</span>,</span><br><span class="line">        &#125;,</span><br><span class="line">        &#123;</span><br><span class="line">          name: <span class="string">&#x27;About&#x27;</span>,</span><br><span class="line">          link: <span class="string">&#x27;https://weibo.com/zceme&#x27;</span>,</span><br><span class="line">        &#125;,</span><br><span class="line">        &#123;</span><br><span class="line">          name: <span class="string">&#x27;divider&#x27;</span>,</span><br><span class="line">        &#125;,</span><br><span class="line">        &#123;</span><br><span class="line">          name: <span class="string">&#x27;About&#x27;</span>,</span><br><span class="line">          link: <span class="string">&#x27;https://github.com/zce&#x27;</span>,</span><br><span class="line">        &#125;,</span><br><span class="line">      ],</span><br><span class="line">    &#125;,</span><br><span class="line">  ],</span><br><span class="line">  pkg: <span class="built_in">require</span>(<span class="string">&#x27;./package.json&#x27;</span>),</span><br><span class="line">  date: <span class="keyword">new</span> <span class="built_in">Date</span>(),</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> page = <span class="function">() =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">return</span> src(<span class="string">`src/**/*.html`</span>, &#123; <span class="attr">base</span>: <span class="string">&#x27;src&#x27;</span> &#125;).pipe(swig(&#123; data &#125;)).pipe(dest(<span class="string">&#x27;dist&#x27;</span>))</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> compile = parallel(style, script, page)</span><br><span class="line"></span><br><span class="line"><span class="built_in">module</span>.exports = &#123;</span><br><span class="line">  compile</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="图片和字体文件的转换"><a href="#图片和字体文件的转换" class="headerlink" title="图片和字体文件的转换"></a>图片和字体文件的转换</h3><p>安装 <code>gulp-imagemin</code></p>
<ul>
<li>内部依赖的模块也是通过 C++ 模块，需要下载二进制程序集（大部分 GitHub 下载），但是这个没办法配置镜像</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yarn add gulp-imagemin --dev</span><br></pre></td></tr></table></figure>

<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> &#123; src, dest, parallel &#125; = <span class="built_in">require</span>(<span class="string">&#x27;gulp&#x27;</span>)</span><br><span class="line"><span class="keyword">const</span> imagemin = <span class="built_in">require</span>(<span class="string">&#x27;gulp-imagemin&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> image = <span class="function">() =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">return</span> src(<span class="string">`src/assets/images/**`</span>, &#123; <span class="attr">base</span>: <span class="string">&#x27;src&#x27;</span> &#125;).pipe(imagemin()).pipe(dest(<span class="string">&#x27;dist&#x27;</span>))</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> font = <span class="function">() =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">return</span> src(<span class="string">`src/assets/fonts/**`</span>, &#123; <span class="attr">base</span>: <span class="string">&#x27;src&#x27;</span> &#125;).pipe(imagemin()).pipe(dest(<span class="string">&#x27;dist&#x27;</span>))</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> compile = parallel(style, script, page, image, font)</span><br><span class="line"></span><br><span class="line"><span class="built_in">module</span>.exports = &#123;</span><br><span class="line">  compile</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="其它文件及文件清除"><a href="#其它文件及文件清除" class="headerlink" title="其它文件及文件清除"></a>其它文件及文件清除</h3><p>安装 <code>del</code></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yarn add del --dev</span><br></pre></td></tr></table></figure>

<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> &#123; src, dest, parallel, series &#125; = <span class="built_in">require</span>(<span class="string">&#x27;gulp&#x27;</span>)</span><br><span class="line"><span class="keyword">const</span> del = <span class="built_in">require</span>(<span class="string">&#x27;del&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> clean = <span class="function">() =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">return</span> del([<span class="string">&#x27;dist&#x27;</span>])</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> compile = parallel(style, script, page, image, font)</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> build = series(clean, parallel(compile, extra))</span><br><span class="line"></span><br><span class="line"><span class="built_in">module</span>.exports = &#123;</span><br><span class="line">  build,</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="自动加载插件"><a href="#自动加载插件" class="headerlink" title="自动加载插件"></a>自动加载插件</h3><p>安装 <code>gulp-load-plugins</code></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yarn add gulp-load-plugins --dev</span><br></pre></td></tr></table></figure>

<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> &#123; src, dest, parallel, series &#125; = <span class="built_in">require</span>(<span class="string">&#x27;gulp&#x27;</span>)</span><br><span class="line"><span class="keyword">const</span> del = <span class="built_in">require</span>(<span class="string">&#x27;del&#x27;</span>)</span><br><span class="line"><span class="keyword">const</span> loadPlugins = <span class="built_in">require</span>(<span class="string">&#x27;gulp-load-plugins&#x27;</span>)</span><br><span class="line"><span class="keyword">const</span> plugins = loadPlugins()</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> data = &#123;...&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> clean = <span class="function">() =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">return</span> del([<span class="string">&#x27;dist&#x27;</span>])</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> style = <span class="function">() =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">return</span> src(<span class="string">`src/assets/styles/*.scss`</span>, &#123; <span class="attr">base</span>: <span class="string">&#x27;src&#x27;</span> &#125;)</span><br><span class="line">    .pipe(plugins.sass(&#123; <span class="attr">outputStyle</span>: <span class="string">&#x27;expanded&#x27;</span> &#125;))</span><br><span class="line">    .pipe(dest(<span class="string">&#x27;dist&#x27;</span>))</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> script = <span class="function">() =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">return</span> src(<span class="string">`src/assets/scripts/*.js`</span>, &#123; <span class="attr">base</span>: <span class="string">&#x27;src&#x27;</span> &#125;)</span><br><span class="line">    .pipe(plugins.babel(&#123; <span class="attr">presets</span>: [<span class="string">&#x27;@babel/preset-env&#x27;</span>] &#125;))</span><br><span class="line">    .pipe(dest(<span class="string">&#x27;dist&#x27;</span>))</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> page = <span class="function">() =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">return</span> src(<span class="string">`src/**/*.html`</span>, &#123; <span class="attr">base</span>: <span class="string">&#x27;src&#x27;</span> &#125;).pipe(plugins.swig(&#123; data &#125;)).pipe(dest(<span class="string">&#x27;dist&#x27;</span>))</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> image = <span class="function">() =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">return</span> src(<span class="string">`src/assets/images/**`</span>, &#123; <span class="attr">base</span>: <span class="string">&#x27;src&#x27;</span> &#125;).pipe(plugins.imagemin()).pipe(dest(<span class="string">&#x27;dist&#x27;</span>))</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> font = <span class="function">() =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">return</span> src(<span class="string">`src/assets/fonts/**`</span>, &#123; <span class="attr">base</span>: <span class="string">&#x27;src&#x27;</span> &#125;).pipe(plugins.imagemin()).pipe(dest(<span class="string">&#x27;dist&#x27;</span>))</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> extra = <span class="function">() =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">return</span> src(<span class="string">`public/**`</span>, &#123; <span class="attr">base</span>: <span class="string">&#x27;public&#x27;</span> &#125;).pipe(dest(<span class="string">&#x27;dist&#x27;</span>))</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> compile = parallel(style, script, page, image, font)</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> build = series(clean, parallel(compile, extra))</span><br><span class="line"></span><br><span class="line"><span class="built_in">module</span>.exports = &#123;</span><br><span class="line">  build,</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="热更新开发服务器"><a href="#热更新开发服务器" class="headerlink" title="热更新开发服务器"></a>热更新开发服务器</h3><p>安装 <code>browser-sync</code></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yarn add browser-sync --dev</span><br></pre></td></tr></table></figure>

<ul>
<li><p>编译过程中并没有处理 <code>node_modules</code> 模块下面的拷贝</p>
<p>可以给 <code>browser-sync</code> 增加一个单独的路由（<code>routes</code>），这个路由优先级比 <code>baseDir</code> 高</p>
</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> browserSync = <span class="built_in">require</span>(<span class="string">&#x27;browser-sync&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> bs = browserSync.create()</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> serve = <span class="function">() =&gt;</span> &#123;</span><br><span class="line">  bs.init(&#123;</span><br><span class="line">    notify: <span class="literal">false</span>,</span><br><span class="line">    port: <span class="number">2080</span>,</span><br><span class="line">    <span class="comment">// open: false,</span></span><br><span class="line">    files: <span class="string">&#x27;dist/**&#x27;</span>,</span><br><span class="line">    server: &#123;</span><br><span class="line">      baseDir: <span class="string">&#x27;dist&#x27;</span>,</span><br><span class="line">      routes: &#123;</span><br><span class="line">        <span class="string">&#x27;/node_modules&#x27;</span>: <span class="string">&#x27;node_modules&#x27;</span>,</span><br><span class="line">      &#125;,</span><br><span class="line">    &#125;,</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="built_in">module</span>.exports = &#123;</span><br><span class="line">  serve,</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="监听变化及构建优化"><a href="#监听变化及构建优化" class="headerlink" title="监听变化及构建优化"></a>监听变化及构建优化</h3><ul>
<li><p><code>style</code>、<code>script</code>、<code>page</code> 文件改变可以 <code>.pipe(bs.reload(&#123; stream: true &#125;))</code></p>
<p>这样 <code>files: &#39;dist/**&#39;</code> 就不需要了 </p>
</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> &#123; src, dest, parallel, series, watch &#125; = <span class="built_in">require</span>(<span class="string">&#x27;gulp&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> style = <span class="function">() =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">return</span> src(<span class="string">`src/assets/styles/*.scss`</span>, &#123; <span class="attr">base</span>: <span class="string">&#x27;src&#x27;</span> &#125;)</span><br><span class="line">    .pipe(plugins.sass(&#123; <span class="attr">outputStyle</span>: <span class="string">&#x27;expanded&#x27;</span> &#125;))</span><br><span class="line">    .pipe(dest(<span class="string">&#x27;dist&#x27;</span>))</span><br><span class="line">    .pipe(bs.reload(&#123; <span class="attr">stream</span>: <span class="literal">true</span> &#125;))</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> script = <span class="function">() =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">return</span> src(<span class="string">`src/assets/scripts/*.js`</span>, &#123; <span class="attr">base</span>: <span class="string">&#x27;src&#x27;</span> &#125;)</span><br><span class="line">    .pipe(plugins.babel(&#123; <span class="attr">presets</span>: [<span class="string">&#x27;@babel/preset-env&#x27;</span>] &#125;))</span><br><span class="line">    .pipe(dest(<span class="string">&#x27;dist&#x27;</span>))</span><br><span class="line">    .pipe(bs.reload(&#123; <span class="attr">stream</span>: <span class="literal">true</span> &#125;))</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> page = <span class="function">() =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">return</span> src(<span class="string">`src/**/*.html`</span>, &#123; <span class="attr">base</span>: <span class="string">&#x27;src&#x27;</span> &#125;)</span><br><span class="line">    .pipe(plugins.swig(&#123; data &#125;))</span><br><span class="line">    .pipe(dest(<span class="string">&#x27;dist&#x27;</span>))</span><br><span class="line">    .pipe(bs.reload(&#123; <span class="attr">stream</span>: <span class="literal">true</span> &#125;))</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> serve = <span class="function">() =&gt;</span> &#123;</span><br><span class="line">  watch(<span class="string">&#x27;src/assets/styles/*.scss&#x27;</span>, style)</span><br><span class="line">  watch(<span class="string">&#x27;src/assets/scripts/*.js&#x27;</span>, script)</span><br><span class="line">  watch(<span class="string">&#x27;src/*.html&#x27;</span>, page)</span><br><span class="line">  <span class="comment">// watch(&#x27;src/assets/images/**&#x27;, image)</span></span><br><span class="line">  <span class="comment">// watch(&#x27;src/assets/fonts/**&#x27;, font)</span></span><br><span class="line">  <span class="comment">// watch(&#x27;public/**&#x27;, extra)</span></span><br><span class="line">  watch([<span class="string">&#x27;src/assets/images/**&#x27;</span>, <span class="string">&#x27;src/assets/fonts/**&#x27;</span>, <span class="string">&#x27;public/**&#x27;</span>], bs.reload)</span><br><span class="line"></span><br><span class="line">  bs.init(&#123;</span><br><span class="line">    notify: <span class="literal">false</span>,</span><br><span class="line">    port: <span class="number">2080</span>,</span><br><span class="line">    <span class="comment">// open: false,</span></span><br><span class="line">    <span class="comment">// files: &#x27;dist/**&#x27;,</span></span><br><span class="line">    server: &#123;</span><br><span class="line">      baseDir: [<span class="string">&#x27;dist&#x27;</span>, <span class="string">&#x27;src&#x27;</span>, <span class="string">&#x27;public&#x27;</span>],</span><br><span class="line">      routes: &#123;</span><br><span class="line">        <span class="string">&#x27;/node_modules&#x27;</span>: <span class="string">&#x27;node_modules&#x27;</span>,</span><br><span class="line">      &#125;,</span><br><span class="line">    &#125;,</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> compile = parallel(style, script, page)</span><br><span class="line"><span class="keyword">const</span> build = series(clean, parallel(compile, image, font, extra))</span><br><span class="line"><span class="keyword">const</span> develop = series(compile, serve)</span><br></pre></td></tr></table></figure>

<h3 id="useref-文件引用处理"><a href="#useref-文件引用处理" class="headerlink" title="useref 文件引用处理"></a>useref 文件引用处理</h3><p>有些文件依赖的是 <code>node_modules</code> 下的文件，这里并没有将其拷贝到 dist 目录，如果上线肯定找不到，本地能找到是因为做了路由映射</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yarn add gulp-useref --dev</span><br></pre></td></tr></table></figure>

<p>构建注释</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- build:css assets/styles/vendor.css --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">link</span> <span class="attr">rel</span>=<span class="string">&quot;stylesheet&quot;</span> <span class="attr">href</span>=<span class="string">&quot;/node_modules/bootstrap/dist/css/bootstrap.css&quot;</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!-- endbuild --&gt;</span></span><br><span class="line"><span class="comment">&lt;!-- build:css assets/styles/main.css --&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">link</span> <span class="attr">rel</span>=<span class="string">&quot;stylesheet&quot;</span> <span class="attr">href</span>=<span class="string">&quot;assets/styles/main.css&quot;</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!-- endbuild --&gt;</span></span><br></pre></td></tr></table></figure>

<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> useref = <span class="function">() =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">return</span> src(<span class="string">&#x27;dist/*.html&#x27;</span>, &#123; <span class="attr">base</span>: <span class="string">&#x27;dist&#x27;</span> &#125;)</span><br><span class="line">    .pipe(plugins.useref(&#123; <span class="attr">searchPath</span>: [<span class="string">&#x27;dist&#x27;</span>, <span class="string">&#x27;.&#x27;</span>] &#125;))</span><br><span class="line">    .pipe(dest(<span class="string">&#x27;dist&#x27;</span>))</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>分别压缩 HTML、CSS、JS</p>
<ul>
<li>另外还需要判断对不同文件的操作，这个时候就需要一个额外的插件去判断</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">yarn add gulp-htmlmin gulp-uglify gulp-clean-css --dev</span><br><span class="line">yarn add gulp-if --dev</span><br></pre></td></tr></table></figure>

<p>操作时会发现 <code>main.css</code> 没有内容，这时因为文件读写冲突，一边读一边写</p>
<ul>
<li>可以把最终转换的结果放在其他目录中</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> useref = <span class="function">() =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">return</span> src(<span class="string">&#x27;dist/*.html&#x27;</span>, &#123; <span class="attr">base</span>: <span class="string">&#x27;dist&#x27;</span> &#125;)</span><br><span class="line">    .pipe(plugins.useref(&#123; <span class="attr">searchPath</span>: [<span class="string">&#x27;dist&#x27;</span>, <span class="string">&#x27;.&#x27;</span>] &#125;))</span><br><span class="line">    .pipe(plugins.if(<span class="regexp">/\.js$/</span>, plugins.uglify()))</span><br><span class="line">    .pipe(plugins.if(<span class="regexp">/\.css$/</span>, plugins.cleanCss()))</span><br><span class="line">    .pipe(</span><br><span class="line">      plugins.if(</span><br><span class="line">        /\.html$/,</span><br><span class="line">        plugins.htmlmin(&#123;</span><br><span class="line">          collapseWhitespace: <span class="literal">true</span>,</span><br><span class="line">          minifyCSS: <span class="literal">true</span>,</span><br><span class="line">          minifyJS: <span class="literal">true</span>,</span><br><span class="line">        &#125;)</span><br><span class="line">      )</span><br><span class="line">    )</span><br><span class="line">    .pipe(dest(<span class="string">&#x27;release&#x27;</span>))</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="重新规划构建过程"><a href="#重新规划构建过程" class="headerlink" title="重新规划构建过程"></a>重新规划构建过程</h3><p>useref 打破了我们的目录结构</p>
<ul>
<li>打包上线是 dist 目录，而 useref 把目录改成了 release</li>
</ul>
<p>增加临时目录 temp</p>
<ol>
<li><code>script</code>、<code>style</code>、<code>page</code> 放到 temp 目录</li>
<li><code>image</code>、<code>font</code>、<code>extra</code> 不需要改，这三个只需要 build 时候去做，只有被 useref 影响才需要</li>
<li><code>serve</code> 里的 baseDir 需要改为 temp</li>
<li><code>useref</code> src 从 temp 里取，把结果放到 <code>dist</code> 目录里</li>
</ol>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> &#123; src, dest, parallel, series, watch &#125; = <span class="built_in">require</span>(<span class="string">&#x27;gulp&#x27;</span>)</span><br><span class="line"><span class="keyword">const</span> del = <span class="built_in">require</span>(<span class="string">&#x27;del&#x27;</span>)</span><br><span class="line"><span class="keyword">const</span> browserSync = <span class="built_in">require</span>(<span class="string">&#x27;browser-sync&#x27;</span>)</span><br><span class="line"><span class="keyword">const</span> loadPlugins = <span class="built_in">require</span>(<span class="string">&#x27;gulp-load-plugins&#x27;</span>)</span><br><span class="line"><span class="keyword">const</span> plugins = loadPlugins()</span><br><span class="line"><span class="keyword">const</span> bs = browserSync.create()</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> clean = <span class="function">() =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">return</span> del([<span class="string">&#x27;dist&#x27;</span>, <span class="string">&#x27;temp&#x27;</span>])</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> style = <span class="function">() =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">return</span> src(<span class="string">&#x27;src/assets/styles/*.scss&#x27;</span>, &#123; <span class="attr">base</span>: <span class="string">&#x27;src&#x27;</span> &#125;)</span><br><span class="line">    .pipe(plugins.sass(&#123; <span class="attr">outputStyle</span>: <span class="string">&#x27;expanded&#x27;</span> &#125;))</span><br><span class="line">    .pipe(dest(<span class="string">&#x27;temp&#x27;</span>))</span><br><span class="line">    .pipe(bs.reload(&#123; <span class="attr">stream</span>: <span class="literal">true</span> &#125;))</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> script = <span class="function">() =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">return</span> src(<span class="string">&#x27;src/assets/scripts/*.js&#x27;</span>, &#123; <span class="attr">base</span>: <span class="string">&#x27;src&#x27;</span> &#125;)</span><br><span class="line">    .pipe(plugins.babel(&#123; <span class="attr">presets</span>: [<span class="string">&#x27;@babel/preset-env&#x27;</span>] &#125;))</span><br><span class="line">    .pipe(dest(<span class="string">&#x27;temp&#x27;</span>))</span><br><span class="line">    .pipe(bs.reload(&#123; <span class="attr">stream</span>: <span class="literal">true</span> &#125;))</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> page = <span class="function">() =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">return</span> src(<span class="string">&#x27;src/*.html&#x27;</span>, &#123; <span class="attr">base</span>: <span class="string">&#x27;src&#x27;</span> &#125;)</span><br><span class="line">    .pipe(plugins.swig(&#123; data, <span class="attr">defaults</span>: &#123; <span class="attr">cache</span>: <span class="literal">false</span> &#125; &#125;)) <span class="comment">// 防止模板缓存导致页面不能及时更新</span></span><br><span class="line">    .pipe(dest(<span class="string">&#x27;temp&#x27;</span>))</span><br><span class="line">    .pipe(bs.reload(&#123; <span class="attr">stream</span>: <span class="literal">true</span> &#125;))</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> image = <span class="function">() =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">return</span> src(<span class="string">&#x27;src/assets/images/**&#x27;</span>, &#123; <span class="attr">base</span>: <span class="string">&#x27;src&#x27;</span> &#125;)</span><br><span class="line">    .pipe(plugins.imagemin())</span><br><span class="line">    .pipe(dest(<span class="string">&#x27;dist&#x27;</span>))</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> font = <span class="function">() =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">return</span> src(<span class="string">&#x27;src/assets/fonts/**&#x27;</span>, &#123; <span class="attr">base</span>: <span class="string">&#x27;src&#x27;</span> &#125;)</span><br><span class="line">    .pipe(plugins.imagemin())</span><br><span class="line">    .pipe(dest(<span class="string">&#x27;dist&#x27;</span>))</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> extra = <span class="function">() =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">return</span> src(<span class="string">&#x27;public/**&#x27;</span>, &#123; <span class="attr">base</span>: <span class="string">&#x27;public&#x27;</span> &#125;)</span><br><span class="line">    .pipe(dest(<span class="string">&#x27;dist&#x27;</span>))</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> serve = <span class="function">() =&gt;</span> &#123;</span><br><span class="line">  watch(<span class="string">&#x27;src/assets/styles/*.scss&#x27;</span>, style)</span><br><span class="line">  watch(<span class="string">&#x27;src/assets/scripts/*.js&#x27;</span>, script)</span><br><span class="line">  watch(<span class="string">&#x27;src/*.html&#x27;</span>, page)</span><br><span class="line">  <span class="comment">// watch(&#x27;src/assets/images/**&#x27;, image)</span></span><br><span class="line">  <span class="comment">// watch(&#x27;src/assets/fonts/**&#x27;, font)</span></span><br><span class="line">  <span class="comment">// watch(&#x27;public/**&#x27;, extra)</span></span><br><span class="line">  watch([</span><br><span class="line">    <span class="string">&#x27;src/assets/images/**&#x27;</span>,</span><br><span class="line">    <span class="string">&#x27;src/assets/fonts/**&#x27;</span>,</span><br><span class="line">    <span class="string">&#x27;public/**&#x27;</span></span><br><span class="line">  ], bs.reload)</span><br><span class="line"></span><br><span class="line">  bs.init(&#123;</span><br><span class="line">    notify: <span class="literal">false</span>,</span><br><span class="line">    port: <span class="number">2080</span>,</span><br><span class="line">    <span class="comment">// open: false,</span></span><br><span class="line">    <span class="comment">// files: &#x27;dist/**&#x27;,</span></span><br><span class="line">    server: &#123;</span><br><span class="line">      baseDir: [<span class="string">&#x27;temp&#x27;</span>, <span class="string">&#x27;src&#x27;</span>, <span class="string">&#x27;public&#x27;</span>],</span><br><span class="line">      routes: &#123;</span><br><span class="line">        <span class="string">&#x27;/node_modules&#x27;</span>: <span class="string">&#x27;node_modules&#x27;</span></span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> useref = <span class="function">() =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">return</span> src(<span class="string">&#x27;temp/*.html&#x27;</span>, &#123; <span class="attr">base</span>: <span class="string">&#x27;temp&#x27;</span> &#125;)</span><br><span class="line">    .pipe(plugins.useref(&#123; <span class="attr">searchPath</span>: [<span class="string">&#x27;temp&#x27;</span>, <span class="string">&#x27;.&#x27;</span>] &#125;))</span><br><span class="line">    <span class="comment">// html js css</span></span><br><span class="line">    .pipe(plugins.if(<span class="regexp">/\.js$/</span>, plugins.uglify()))</span><br><span class="line">    .pipe(plugins.if(<span class="regexp">/\.css$/</span>, plugins.cleanCss()))</span><br><span class="line">    .pipe(plugins.if(<span class="regexp">/\.html$/</span>, plugins.htmlmin(&#123;</span><br><span class="line">      collapseWhitespace: <span class="literal">true</span>,</span><br><span class="line">      minifyCSS: <span class="literal">true</span>,</span><br><span class="line">      minifyJS: <span class="literal">true</span></span><br><span class="line">    &#125;)))</span><br><span class="line">    .pipe(dest(<span class="string">&#x27;dist&#x27;</span>))</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> compile = parallel(style, script, page)</span><br><span class="line"><span class="keyword">const</span> build =  series(</span><br><span class="line">  clean,</span><br><span class="line">  parallel(</span><br><span class="line">    series(compile, useref),</span><br><span class="line">    image,</span><br><span class="line">    font,</span><br><span class="line">    extra</span><br><span class="line">  )</span><br><span class="line">)</span><br><span class="line"><span class="keyword">const</span> develop = series(compile, serve)</span><br><span class="line"></span><br><span class="line"><span class="built_in">module</span>.exports = &#123;</span><br><span class="line">  clean,</span><br><span class="line">  build,</span><br><span class="line">  develop</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>构建完这套系统，如果你不想写文档说明，导出几个常用即可，<code>clean</code>、<code>build</code>、<code>develop</code></p>
<ul>
<li>并在 <code>package.json</code> 中增加 script 命令</li>
</ul>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">&quot;scripts&quot;: &#123;</span><br><span class="line">  &quot;clean&quot;: &quot;gulp clean&quot;,</span><br><span class="line">  &quot;build&quot;: &quot;gulp build&quot;,</span><br><span class="line">  &quot;develop&quot;: &quot;gulp develop&quot;</span><br><span class="line">&#125;,</span><br></pre></td></tr></table></figure>

<h2 id="封装工作流"><a href="#封装工作流" class="headerlink" title="封装工作流"></a>封装工作流</h2><p>提取多个项目共同的自动化构建过程（Don’t repeat yourself)</p>
<p><img data-src="https://gitee.com/lilyn/pic/raw/master/lagoulearn-img/gulp%E8%87%AA%E5%8A%A8%E5%8C%96%E5%B7%A5%E4%BD%9C%E6%B5%81%E6%9E%84%E5%BB%BA.png" alt="gulp自动化工作流构建"></p>
<h3 id="提取-gulpfile"><a href="#提取-gulpfile" class="headerlink" title="提取 gulpfile"></a>提取 gulpfile</h3><p><code>zce-gulp-demo</code> 这里叫 A，<code>zce-pages</code> 这里叫 B</p>
<ul>
<li>在命令行输入 <code>code . -a</code> 在同一窗口打开这个项目</li>
</ul>
<p>把 A 项目工作流提取到 B 项目中，操作步骤如下：</p>
<ol>
<li>把 A 的 <code>gulpfile.js</code> 内容复制到 B 的 <code>lib/index.js</code></li>
<li>把 A 的 <code>package.json</code> 中的 <code>devDependencies</code> 复制到 B 中 <code>dependencies</code></li>
<li>把 A 的 <code>package.json</code> 中的 <code>devDependencies</code> 全部删除，并把 <code>node_modules</code> 全部删除</li>
<li>把 B link 到全局，切换到 A 输入命令 <code>yarn link zce-pages</code>，B 项目会通过软链接形式链过来</li>
<li>在 B 中新建 <code>gulpfile.js</code> 新增 <code>module.exports = require(&#39;zce-pages&#39;)</code> 内容</li>
<li>执行 <code>yarn build</code></li>
</ol>
<p>执行 <code>yarn build</code> 我这边报错了，是因为 <code>gulp-sass</code> 没有下载下来</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">Error <span class="keyword">in</span> plugin <span class="string">&quot;gulp-sass&quot;</span></span><br><span class="line">Message:</span><br><span class="line"></span><br><span class="line">gulp-sass no longer has a default Sass compiler; please <span class="built_in">set</span> one yourself.</span><br><span class="line">Both the <span class="string">&quot;sass&quot;</span> and <span class="string">&quot;node-sass&quot;</span> packages are permitted.</span><br></pre></td></tr></table></figure>

<p>解决：安装 <code>sass</code> </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yarn add sass</span><br></pre></td></tr></table></figure>

<p>修改 <code>lib/index.js</code></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> sass = <span class="built_in">require</span>(<span class="string">&#x27;gulp-sass&#x27;</span>)(<span class="built_in">require</span>(<span class="string">&#x27;sass&#x27;</span>))</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> style = <span class="function">() =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">return</span> src(<span class="string">&#x27;src/assets/styles/*.scss&#x27;</span>, &#123; <span class="attr">base</span>: <span class="string">&#x27;src&#x27;</span> &#125;)</span><br><span class="line">    .pipe(sass(&#123; <span class="attr">outputStyle</span>: <span class="string">&#x27;expanded&#x27;</span> &#125;))</span><br><span class="line">    .pipe(dest(<span class="string">&#x27;temp&#x27;</span>))</span><br><span class="line">    .pipe(bs.reload(&#123; <span class="attr">stream</span>: <span class="literal">true</span> &#125;))</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="抽象路径配置"><a href="#抽象路径配置" class="headerlink" title="抽象路径配置"></a>抽象路径配置</h3><p>对于代码里写死的路径，如 sass 的路径，这样不是很灵活，我们可以抽象一下形成配置</p>
<ul>
<li><p><code>cwd</code> 返回命令行工作目录，然后拿到工作文件 <code>pages.config.js</code></p>
<p><code>require</code> 不存在的目录会报错，所以这里用 <code>try...catch...</code> 包裹一下</p>
</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> cwd = process.cwd()</span><br><span class="line"><span class="keyword">let</span> config = &#123;</span><br><span class="line">  build: &#123;</span><br><span class="line">    src: <span class="string">&#x27;src&#x27;</span>,</span><br><span class="line">    dist: <span class="string">&#x27;dist&#x27;</span>,</span><br><span class="line">    temp: <span class="string">&#x27;temp&#x27;</span>,</span><br><span class="line">    public: <span class="string">&#x27;public&#x27;</span>,</span><br><span class="line">    paths: &#123;</span><br><span class="line">      styles: <span class="string">&#x27;assets/styles/*.scss&#x27;</span>,</span><br><span class="line">      scripts: <span class="string">&#x27;assets/scripts/*.js&#x27;</span>,</span><br><span class="line">      pages: <span class="string">&#x27;*.html&#x27;</span>,</span><br><span class="line">      images: <span class="string">&#x27;assets/images/**&#x27;</span>,</span><br><span class="line">      fonts: <span class="string">&#x27;assets/fonts/**&#x27;</span></span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">try</span> &#123;</span><br><span class="line">  <span class="keyword">const</span> loadConfig = <span class="built_in">require</span>(<span class="string">`<span class="subst">$&#123;cwd&#125;</span>/pages.config.js`</span>)</span><br><span class="line">  config = <span class="built_in">Object</span>.assign(&#123;&#125;, config, loadConfig)</span><br><span class="line">&#125; <span class="keyword">catch</span> (e) &#123;&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> style = <span class="function">() =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">return</span> src(config.build.paths.styles, &#123; <span class="attr">base</span>: config.build.src, <span class="attr">cwd</span>: config.build.src &#125;)</span><br><span class="line">    .pipe(sass(&#123; <span class="attr">outputStyle</span>: <span class="string">&#x27;expanded&#x27;</span> &#125;))</span><br><span class="line">    .pipe(dest(config.build.temp))</span><br><span class="line">    .pipe(bs.reload(&#123; <span class="attr">stream</span>: <span class="literal">true</span> &#125;))</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="包装-Gulp-Cli"><a href="#包装-Gulp-Cli" class="headerlink" title="包装 Gulp Cli"></a>包装 Gulp Cli</h3><p><code>gulpfile.js</code> 里只是写了个 <code>module.exports = require(&#39;zce-pages&#39;)</code>，其实有点冗余，我们希望没有 <code>gulpfile.js</code> 也能正常工作</p>
<ul>
<li>这里需要制定一下工作目录为当前</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yarn gulp build --gulpfile ./node_modules/zce-pages/lib/index.js --cwd .</span><br></pre></td></tr></table></figure>

<p>不过这么多参数就很复杂了，可以集成 cli</p>
<ol>
<li><p>创建 <code>bin/zce-pages.js</code> 作为 cli 执行入口</p>
</li>
<li><p>在 <code>package.json</code> 中增加 <code>bin</code> 字段</p>
<figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&quot;bin&quot;: &quot;bin/zce-pages.js&quot;,</span><br></pre></td></tr></table></figure>

</li>
</ol>
<p>修改 <code>zce-pages.js</code></p>
<ul>
<li><code>process.argv</code> 是一个数组</li>
<li><code>require</code> 是载入这个模块，<code>require.resolve</code> 是找到这个模块所在的路径</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#!/usr/bin/env node</span></span><br><span class="line"></span><br><span class="line">process.argv.push(<span class="string">&#x27;--cwd&#x27;</span>)</span><br><span class="line">process.argv.push(process.cwd())</span><br><span class="line">process.argv.push(<span class="string">&#x27;--gulpfile&#x27;</span>)</span><br><span class="line">process.argv.push(<span class="built_in">require</span>.resolve(<span class="string">&#x27;..&#x27;</span>))</span><br><span class="line"></span><br><span class="line"><span class="built_in">require</span>(<span class="string">&#x27;gulp/bin/gulp&#x27;</span>)</span><br></pre></td></tr></table></figure>

<p>代码提交完，就可以使用 publish 提交 git</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yarn publish --registry https://registry.yarnpkg.com</span><br></pre></td></tr></table></figure>

<p>同步时可能会有问题，因为你发布的是 npm 源，但是你是从淘宝源下载，可能会存在同步问题，可以进入淘宝镜像搜一下你 publish 的包，然后更新</p>
<h3 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h3><ol>
<li><p>新建项目如果想要使用工作流，直接运行 <code>package.json</code> 中的命令即可</p>
</li>
<li><p>创建的 cli 是 <code>node_modules/bin/zce-pages.cmd</code>，这个文件中是通过 node 去执行 <code>zce-pages/bin/zce-pages.js</code></p>
</li>
<li><p>在 <code>node_modules</code> 找到 <code>zce-pages/bin/zce-pages.js</code>，里面就是告诉 gulp 工作时的工作目录和 gulpfile 的工作目录</p>
<p>之后执行 <code>gulp/bin/gulp</code> 就可以执行 gulpifle 了，完成整个工作流过程</p>
</li>
</ol>
<h2 id="FIS"><a href="#FIS" class="headerlink" title="FIS"></a>FIS</h2><p>高度集成，只需要简单配置</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">yarn global add fis3</span><br></pre></td></tr></table></figure>

<p>relase 解决的资源定位</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">fis3 realse -d output</span><br></pre></td></tr></table></figure>

<p>新建 <code>fis-conf.js</code></p>
<ul>
<li><p>编译与压缩</p>
<p>安装 <code>yarn add fis-parser-node-sass</code>、<code>yarn add fis-parser-babel-6.x</code></p>
</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">fis.match(<span class="string">&#x27;*.&#123;js,scss,png&#125;&#x27;</span>, &#123;</span><br><span class="line">  release: <span class="string">&#x27;/assets/$0&#x27;</span>,</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line">fis.match(<span class="string">&#x27;**/*.scss&#x27;</span>, &#123;</span><br><span class="line">  rExt: <span class="string">&#x27;.css&#x27;</span>,</span><br><span class="line">  parser: fis.plugin(<span class="string">&#x27;node-sass&#x27;</span>),</span><br><span class="line">  optimizer: fis.plugin(<span class="string">&#x27;clean-css&#x27;</span>),</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line">fis.match(<span class="string">&#x27;**/*.js&#x27;</span>, &#123;</span><br><span class="line">  parser: fis.plugin(<span class="string">&#x27;babel-6.x&#x27;</span>),</span><br><span class="line">  optimizer: fis.plugin(<span class="string">&#x27;uglify-js&#x27;</span>),</span><br><span class="line">&#125;)</span><br></pre></td></tr></table></figure>

<p>查看转换过程中转换的配置</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">fis3 inspect</span><br></pre></td></tr></table></figure>


    </div>

    
    
    

	<div>
		  
			
<div class="my_post_copyright clearfix">
  <script src="//cdn.bootcss.com/clipboard.js/1.5.10/clipboard.min.js"></script>
  
  <!-- JS库 sweetalert 可修改路径 -->
  <script src="https://cdn.bootcss.com/jquery/2.0.0/jquery.min.js"></script>
  <script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
  <article class="box-left">
    <img src="https://gitee.com/lilyn/pic/raw/master/logo.jpg">
  </article>
  <article class="box-right">
    <p><span>本文标题:</span><a href="/posts/95392a2e/">自动化构建（Grunt、Gulp）</a></p>
    <p><span>文章作者:</span><a href="/" title="访问 LiLyn 的个人博客">LiLyn</a></p>
    <p><span>原始链接:</span><a href="/posts/95392a2e/" title="自动化构建（Grunt、Gulp）">http://llmysnow.top/posts/95392a2e/</a>
    <span class="copy-path"  title="点击复制文章链接"><i class="fa fa-clipboard" data-clipboard-text="http://llmysnow.top/posts/95392a2e/"  aria-label="复制成功！"></i></span>
  </p>
    <p><span>许可协议:</span>转载请保留原文链接及作者。</p>  
  </article>
</div>
<script> 
    var clipboard = new Clipboard('.fa-clipboard');
    $(".fa-clipboard").click(function(){
      clipboard.on('success', function(){
        swal({   
          title: "",   
          text: '复制成功',
          icon: "success", 
          showConfirmButton: true
          });
    });
    });  
</script>

		  
	</div>

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/Grunt/" rel="tag"><i class="fa fa-tag"></i> Grunt</a>
              <a href="/tags/Gulp/" rel="tag"><i class="fa fa-tag"></i> Gulp</a>
              <a href="/tags/%E5%B0%81%E8%A3%85%E5%B7%A5%E4%BD%9C%E6%B5%81/" rel="tag"><i class="fa fa-tag"></i> 封装工作流</a>
          </div>
		 
		<!--post-tags 彩色标签-->
		<script type="text/javascript">
		var tagsall=document.getElementsByClassName("post-tags")
		for (var i = tagsall.length - 1; i >= 0; i--){
			var tags=tagsall[i].getElementsByTagName("a");
			for (var j = tags.length - 1; j >= 0; j--) {
				var r=Math.floor(Math.random()*75+130);
				var g=Math.floor(Math.random()*75+100);
				var b=Math.floor(Math.random()*75+80);
				tags[j].style.background = "rgb("+r+","+g+","+b+")";
				tags[j].style.color = "#fff";
				tags[j].style.padding = "0 10px";
			}
		}                        
		</script>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/posts/c7bac255/" rel="prev" title="手撕 Promise（then 链及 Promise 方法实现）">
      <i class="fa fa-chevron-left"></i> 手撕 Promise（then 链及 Promise 方法实现）
    </a></div>
      <div class="post-nav-item"></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%87%AA%E5%8A%A8%E5%8C%96%E6%9E%84%E5%BB%BA"><span class="nav-text">自动化构建</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Grunt"><span class="nav-text">Grunt</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8"><span class="nav-text">基本使用</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%A0%87%E8%AE%B0%E4%BB%BB%E5%8A%A1%E5%A4%B1%E8%B4%A5"><span class="nav-text">标记任务失败</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%85%8D%E7%BD%AE%E9%80%89%E9%A1%B9%E6%96%B9%E6%B3%95"><span class="nav-text">配置选项方法</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%A4%9A%E7%9B%AE%E6%A0%87%E4%BB%BB%E5%8A%A1"><span class="nav-text">多目标任务</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Grunt-%E6%8F%92%E4%BB%B6"><span class="nav-text">Grunt 插件</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Gulp"><span class="nav-text">Gulp</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8-1"><span class="nav-text">基本使用</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%BB%84%E5%90%88%E4%BB%BB%E5%8A%A1"><span class="nav-text">组合任务</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%BC%82%E6%AD%A5%E4%BB%BB%E5%8A%A1"><span class="nav-text">异步任务</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%A0%B8%E5%BF%83%E5%B7%A5%E4%BD%9C%E5%8E%9F%E7%90%86"><span class="nav-text">核心工作原理</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%96%87%E4%BB%B6%E6%93%8D%E4%BD%9C-API"><span class="nav-text">文件操作 API</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Gulp-%E6%A1%88%E4%BE%8B"><span class="nav-text">Gulp 案例</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%A0%B7%E5%BC%8F%E7%BC%96%E8%AF%91"><span class="nav-text">样式编译</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%84%9A%E6%9C%AC%E7%BC%96%E8%AF%91"><span class="nav-text">脚本编译</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%A1%B5%E9%9D%A2%E6%A8%A1%E6%9D%BF%E7%BC%96%E8%AF%91"><span class="nav-text">页面模板编译</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%9B%BE%E7%89%87%E5%92%8C%E5%AD%97%E4%BD%93%E6%96%87%E4%BB%B6%E7%9A%84%E8%BD%AC%E6%8D%A2"><span class="nav-text">图片和字体文件的转换</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%85%B6%E5%AE%83%E6%96%87%E4%BB%B6%E5%8F%8A%E6%96%87%E4%BB%B6%E6%B8%85%E9%99%A4"><span class="nav-text">其它文件及文件清除</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%87%AA%E5%8A%A8%E5%8A%A0%E8%BD%BD%E6%8F%92%E4%BB%B6"><span class="nav-text">自动加载插件</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%83%AD%E6%9B%B4%E6%96%B0%E5%BC%80%E5%8F%91%E6%9C%8D%E5%8A%A1%E5%99%A8"><span class="nav-text">热更新开发服务器</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%9B%91%E5%90%AC%E5%8F%98%E5%8C%96%E5%8F%8A%E6%9E%84%E5%BB%BA%E4%BC%98%E5%8C%96"><span class="nav-text">监听变化及构建优化</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#useref-%E6%96%87%E4%BB%B6%E5%BC%95%E7%94%A8%E5%A4%84%E7%90%86"><span class="nav-text">useref 文件引用处理</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E9%87%8D%E6%96%B0%E8%A7%84%E5%88%92%E6%9E%84%E5%BB%BA%E8%BF%87%E7%A8%8B"><span class="nav-text">重新规划构建过程</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%B0%81%E8%A3%85%E5%B7%A5%E4%BD%9C%E6%B5%81"><span class="nav-text">封装工作流</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%8F%90%E5%8F%96-gulpfile"><span class="nav-text">提取 gulpfile</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%8A%BD%E8%B1%A1%E8%B7%AF%E5%BE%84%E9%85%8D%E7%BD%AE"><span class="nav-text">抽象路径配置</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%8C%85%E8%A3%85-Gulp-Cli"><span class="nav-text">包装 Gulp Cli</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%80%BB%E7%BB%93"><span class="nav-text">总结</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#FIS"><span class="nav-text">FIS</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="LiLyn"
      src="/images/logo.jpg">
  <p class="site-author-name" itemprop="name">LiLyn</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">70</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">10</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">162</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/llwodexue" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;llwodexue" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://blog.csdn.net/qq_38689395" title="CSDN → https:&#x2F;&#x2F;blog.csdn.net&#x2F;qq_38689395" rel="noopener" target="_blank"><i class="fas fa-copyright fa-fw"></i>CSDN</a>
      </span>
  </div>


  <div class="links-of-blogroll motion-element">
    <div class="links-of-blogroll-title"><i class="fa fa-link fa-fw"></i>
      友情链接
    </div>
    <ul class="links-of-blogroll-list">
        <li class="links-of-blogroll-item">
          <a href="https://scx69.gitee.io/blogs/" title="https:&#x2F;&#x2F;scx69.gitee.io&#x2F;blogs&#x2F;" rel="noopener" target="_blank">花开满城</a>
        </li>
        <li class="links-of-blogroll-item">
          <a href="http://yixin-oss.gitee.io/daytoy/" title="http:&#x2F;&#x2F;yixin-oss.gitee.io&#x2F;daytoy&#x2F;" rel="noopener" target="_blank">daytoy</a>
        </li>
    </ul>
  </div>

      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">LiLyn</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-area"></i>
    </span>
    <span title="站点总字数">463k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">7:01</span>
</div>

<!--
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>-->

<!--
<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
<div class="powered-by">
  <i class="fa fa-user-md"></i>
  <span id="busuanzi_container_site_pv">
    本站访问量:<span id="busuanzi_value_site_pv"></span>次
  </span>
  <span class="post-meta-divider">|</span>
  <span id="busuanzi_container_site_uv">
    本站总访客量：<span id="busuanzi_value_site_uv"></span>人
  </span>
</div>-->

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/lozad@1/dist/lozad.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/pisces.js"></script>


<script src="/js/next-boot.js"></script>




  
  <script>
    (function(){
      var canonicalURL, curProtocol;
      //Get the <link> tag
      var x=document.getElementsByTagName("link");
		//Find the last canonical URL
		if(x.length > 0){
			for (i=0;i<x.length;i++){
				if(x[i].rel.toLowerCase() == 'canonical' && x[i].href){
					canonicalURL=x[i].href;
				}
			}
		}
    //Get protocol
	    if (!canonicalURL){
	    	curProtocol = window.location.protocol.split(':')[0];
	    }
	    else{
	    	curProtocol = canonicalURL.split(':')[0];
	    }
      //Get current URL if the canonical URL does not exist
	    if (!canonicalURL) canonicalURL = window.location.href;
	    //Assign script content. Replace current URL with the canonical URL
      !function(){var e=/([http|https]:\/\/[a-zA-Z0-9\_\.]+\.baidu\.com)/gi,r=canonicalURL,t=document.referrer;if(!e.test(r)){var n=(String(curProtocol).toLowerCase() === 'https')?"https://sp0.baidu.com/9_Q4simg2RQJ8t7jm9iCKT-xh_/s.gif":"//api.share.baidu.com/s.gif";t?(n+="?r="+encodeURIComponent(document.referrer),r&&(n+="&l="+r)):r&&(n+="?l="+r);var i=new Image;i.src=n}}(window);})();
  </script>




  
<script src="/js/local-search.js"></script>













  

  

  <!-- 动态线条背景 -->
  <script type="text/javascript" src="//cdn.bootcss.com/canvas-nest.js/1.0.0/canvas-nest.min.js"></script>
  <!-- 页面点击小红心 -->
  <script type="text/javascript">
	!function(e,t,a){function n(){c(".heart{width: 10px;height: 10px;position: fixed;background: #f00;transform: rotate(45deg);-webkit-transform: rotate(45deg);-moz-transform: rotate(45deg);}.heart:after,.heart:before{content: '';width: inherit;height: inherit;background: inherit;border-radius: 50%;-webkit-border-radius: 50%;-moz-border-radius: 50%;position: fixed;}.heart:after{top: -5px;}.heart:before{left: -5px;}"),o(),r()}function r(){for(var e=0;e<d.length;e++)d[e].alpha<=0?(t.body.removeChild(d[e].el),d.splice(e,1)):(d[e].y--,d[e].scale+=.004,d[e].alpha-=.013,d[e].el.style.cssText="left:"+d[e].x+"px;top:"+d[e].y+"px;opacity:"+d[e].alpha+";transform:scale("+d[e].scale+","+d[e].scale+") rotate(45deg);background:"+d[e].color+";z-index:99999");requestAnimationFrame(r)}function o(){var t="function"==typeof e.onclick&&e.onclick;e.onclick=function(e){t&&t(),i(e)}}function i(e){var a=t.createElement("div");a.className="heart",d.push({el:a,x:e.clientX-5,y:e.clientY-5,scale:1,alpha:1,color:s()}),t.body.appendChild(a)}function c(e){var a=t.createElement("style");a.type="text/css";try{a.appendChild(t.createTextNode(e))}catch(t){a.styleSheet.cssText=e}t.getElementsByTagName("head")[0].appendChild(a)}function s(){return"rgb("+~~(255*Math.random())+","+~~(255*Math.random())+","+~~(255*Math.random())+")"}var d=[];e.requestAnimationFrame=function(){return e.requestAnimationFrame||e.webkitRequestAnimationFrame||e.mozRequestAnimationFrame||e.oRequestAnimationFrame||e.msRequestAnimationFrame||function(e){setTimeout(e,1e3/60)}}(),n()}(window,document);
  </script>
<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"log":false,"model":{"jsonPath":"/live2dw/assets/hijiki.model.json"},"display":{"position":"right","width":150,"height":300},"mobile":{"show":true},"react":{"opacity":0.7}});</script></body>
</html>